package com.usian.cart.service.impl;

import com.usian.cart.fegin.ProductFegin;
import com.usian.cart.service.CartService;
import com.usian.jyzx.model.entity.product.ProductSku;
import com.usian.jyzx.model.vo.h5.CartInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
 * @TODO:
 * @Author:baimu
 * @Date:2024 07 11 15 07
 **/
@Service
@Slf4j
public class CartServiceImpl implements CartService {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    ProductFegin productFegin;
    @Override
    public void addCart(Integer userId,Long skuId, Integer num) {
        // 1.判断redis中是否有该商品
        //1.1:获取这个人的所有的购物车数据
        BoundHashOperations boundHashOperations = redisTemplate.boundHashOps("userId:" + userId);
        //1.2:判断这个人的所有的购物车数据 里面有没有这个skuId
        CartInfo cartInfo = (CartInfo)boundHashOperations.get(skuId);
        //1.3:之前没有购买过这个商品，直接添加
        if(null==cartInfo){
            ProductSku productSku = productFegin.findProductSkuById(skuId);

            cartInfo =  new CartInfo();
            cartInfo.setSkuNum(num);
            cartInfo.setIsChecked(1);
            cartInfo.setUserId(userId);
            cartInfo.setCartPrice(productSku.getSalePrice());
            cartInfo.setSkuName(productSku.getSkuName());
            cartInfo.setImgUrl(productSku.getThumbImg());
            cartInfo.setSkuId(skuId);
            boundHashOperations.put(skuId,cartInfo);

        }else{
            //1.4:之前购买过这个商品，更新
            cartInfo.setSkuNum(cartInfo.getSkuNum()+num);
            boundHashOperations.put(skuId,cartInfo);
        }



    }

    @Override
    public List<CartInfo> findCartList(Integer userId) {

        BoundHashOperations boundHashOperations = redisTemplate.boundHashOps("userId:" + userId);
        Object key = boundHashOperations.getKey();
        log.info("key:{}",key);
        List<CartInfo> values = (List<CartInfo>)boundHashOperations.values();
        log.info("values:{}",values);

        return values;
    }


    public void delCart(Integer userId,Long skuId){
        BoundHashOperations boundHashOperations = redisTemplate.boundHashOps("userId:" + userId);

        boundHashOperations.delete(skuId);
    }


    @Override
    public void checkCart(Integer userId,Long skuId,Integer isChecked){
        BoundHashOperations boundHashOperations = redisTemplate.boundHashOps("userId:" + userId);
        CartInfo cartInfo = (CartInfo)boundHashOperations.get(skuId);
        log.info("cartInfo:{}",cartInfo);
        cartInfo.setIsChecked(isChecked);
        boundHashOperations.put(skuId,cartInfo);



    }

    @Override
    public void allcheckCart(Integer userId, Integer isChecked) {
        BoundHashOperations boundHashOperations = redisTemplate.boundHashOps("userId:" + userId);
        List<CartInfo> cartInfos = (List<CartInfo>)boundHashOperations.values();
        log.info("cartInfos:{}",cartInfos);


        cartInfos.forEach(cartInfo -> {
            cartInfo.setIsChecked(isChecked);
            boundHashOperations.put(cartInfo.getSkuId(),cartInfo);
        });



    }

    @Override
    public void clearCart(Integer userId) {
//        BoundHashOperations boundHashOperations = redisTemplate.boundHashOps("userId:" + userId);
//        Object key = boundHashOperations.getKey();
        redisTemplate.delete("userId:" + userId);


    }
}
